package com.doubi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.entity.Classifytable;
import com.doubi.entity.Releasetable;
import com.doubi.entity.Userinfo;
import com.doubi.service.ClassifytableService;
import com.doubi.service.LiketableService;
import com.doubi.service.ReleasetableService;
import com.doubi.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@RestController
@CrossOrigin("*")
public class ClassifytableController {
    @Autowired
    private ClassifytableService classifytableService;
    @Autowired
    private ReleasetableService releasetableService;
    @Autowired
    private LiketableService liketableService;
    @Autowired
    private UserinfoService userinfoService;

    /**
     * 查询所有分类
     */
    @GetMapping("clssifyList")
    public Object clssifyList(){
        return classifytableService.list();
    }

    /**
     * 分类标签查询
     * @return 分类list
     */
    @GetMapping("/getClassify")
    public List<Classifytable> getClassify() {
        List<Classifytable> list = classifytableService.list();
        return list;
    }

    /**
     * 查询我的视频数据分类
     * @return 分类list
     */
    @GetMapping("/myClassifyNum")
    public List<Classifytable> getClassifyNum(Integer userId) {
        //首先先查分类列表
//        QueryWrapper<Releasetable> wrapper = new QueryWrapper<>();
//        wrapper.eq("userId", userId);
        List<Classifytable> list = classifytableService.list();
        //循环查询分类条数
        for (int i=0;i<list.size();i++){
            Integer count = releasetableService.getCount(userId, list.get(i).getId());
            list.get(i).setClassifyNum(count);
        }
        return list;
    }

    /**
     * 获得对应classId的视频
     * @param classifyId 类别id
     * @return 视频列表
     */
    @GetMapping("/getClassifiedVideo")
    public List<Releasetable> getClassifiedVideo(Integer classifyId) throws ParseException {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        List<Releasetable> list;
        if (classifyId == null||classifyId == 0){
            list = releasetableService.list();
        }else {
            QueryWrapper<Releasetable> qw=new QueryWrapper<>();
            qw.eq("classId",classifyId);
            list = releasetableService.list(qw);
        }
        //循环查询出的视频列表id查询点赞数
        for (int i=0;i<list.size();i++){
            //查询视频作者信息
            queryWrapper.eq("userId",list.get(i).getUserId());
            list.get(i).setUserinfo(userinfoService.getOne(queryWrapper));
            int likeNum = liketableService.releaseCountLike(list.get(i).getReleaseId());
            list.get(i).setLikeNum(likeNum);
            //顺便处理时间
            String releaseTime = list.get(i).getReleaseTime();
            LocalDateTime now=LocalDateTime.now();
            DateTimeFormatter d=DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss");
            String nowTime =now.format(d);
            if (releaseTime.substring(0,4).equals(nowTime.substring(0,4))){
                //相同年份
                if (releaseTime.substring(5,7).equals(nowTime.substring(5,7))){
                    //相同月份
                    if (releaseTime.substring(8,10).equals(nowTime.substring(8,10))){
                        //相同日期
                        if (releaseTime.substring(11,12).equals(nowTime.substring(11,12))){
                            //相同时间
                            if (releaseTime.substring(13,14).equals(nowTime.substring(13,14))){
                                //相同分钟
                                list.get(i).setReleaseTime("刚刚");
                            }else {
                                list.get(i).setReleaseTime((Integer.parseInt(nowTime.substring(13,14))-Integer.parseInt(releaseTime.substring(13,14)))+"分钟前");
                            }
                        }else {
                            list.get(i).setReleaseTime((Integer.parseInt(nowTime.substring(11,12))-Integer.parseInt(releaseTime.substring(11,12)))+"小时前");
                        }
                    }else {
                        list.get(i).setReleaseTime((Integer.parseInt(nowTime.substring(8,10))-Integer.parseInt(releaseTime.substring(8,10)))+"天前");
                    }
                }else {
                    list.get(i).setReleaseTime((Integer.parseInt(nowTime.substring(5,7))-Integer.parseInt(releaseTime.substring(5,7)))+"个月前");
                }
            }
        }
        return list;
    }
}
